﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DBO_ShopMob;
using System.Windows.Media;
using WP_Customer_ShopMob.Model;

namespace WP_Customer_ShopMob.Views
{
    public partial class InscriptionPage : PhoneApplicationPage
    {
        //Model.Repository repo = new Model.Repository();
        IRepository repo = Data.GetModel;
        string message;

        public bool isListPickerClosed = true;

        //correction bug ListPicker dans ScrollViewer
        private void lpk_civilite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ListPicker lp = (ListPicker)sender;

            if (isListPickerClosed)
            {
                if (lp.ListPickerMode == ListPickerMode.Normal)
                    lp.Open();

                isListPickerClosed = false;
            }
            else
            {
                isListPickerClosed = true;
            }
        }  

        public InscriptionPage()
        {
            InitializeComponent();
            this.lpk_civilite.ItemsSource = new List<string>() { "Monsieur", "Madame", "Mademoiselle" };
        }

        private bool isModelValid()
        {
            bool ok = true;
            message = "Echec lors de l'inscription. Les valeurs des champs suivants sont manquantes ou incorrectes :";

            // Vérification Nom
            if(String.IsNullOrEmpty(txt_nom.Text))
            {
                txt_nom.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Nom";
            }
            else
            {
                txt_nom.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_nom.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Prénom 
            if (String.IsNullOrEmpty(txt_prenom.Text))
            {
                txt_prenom.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Prénom";
            }
            else
            {
                txt_prenom.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_prenom.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Password 
            if (String.IsNullOrEmpty(txt_password.Password))
            {
                txt_password.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Mot de passe";
            }
            else
            {
                txt_password.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_password.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Email 
            if (String.IsNullOrEmpty(txt_email.Text))
            {
                txt_email.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Email";
            }
            else
            {
                txt_email.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_email.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Adresse 
            if (String.IsNullOrEmpty(txt_adresse.Text))
            {
                txt_adresse.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Adresse";
            }
            else
            {
                txt_adresse.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_adresse.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Code Postal 
            if (String.IsNullOrEmpty(txt_cp.Text))
            {
                txt_cp.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Code postal";
            }
            else
            {
                txt_cp.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_cp.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Ville 
            if (String.IsNullOrEmpty(txt_ville.Text))
            {
                txt_ville.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Ville";
            }
            else
            {
                txt_ville.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_ville.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Téléphone fixe 
            if (String.IsNullOrEmpty(txt_fixe.Text))
            {
                txt_fixe.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Téléphone fixe";
            }
            else
            {
                txt_fixe.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_fixe.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Téléphone mobile 
            if (String.IsNullOrEmpty(txt_mobile.Text))
            {
                txt_mobile.Background = new SolidColorBrush(Color.FromArgb(75, 241, 12, 12));
                ok = false;
                message += "\n - Téléphone mobile";
            }
            else
            {
                txt_mobile.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
                txt_mobile.BorderBrush = new SolidColorBrush(Color.FromArgb(180, 0, 153, 0));
                txt_nom.BorderThickness = new Thickness(3);
            }

            // Vérification Date Naissance 
            if ((DateTime.Today.Year - ((DateTime)dt_picker.Value).Year) < 18)
            {
                ok = false;
                message += "\n - Date de naissance (+ de 18 ans)";
            }
            return ok;
        }

        private void btn_enregistrer(object sender, RoutedEventArgs e)
        {
            if (isModelValid())
            {
                Client client = new Client(null, lpk_civilite.SelectedItem.ToString(), txt_nom.Text, txt_prenom.Text, txt_password.Password, txt_email.Text, txt_adresse.Text, txt_cp.Text, txt_ville.Text, txt_fixe.Text, txt_mobile.Text, (DateTime)dt_picker.Value);
                repo.AjouterClient(client, delegate(string resu, Exception error)
                {
                    if (error != null)
                    {
                        if (resu == "1")
                        {
                            MessageBox.Show("Inscription réussie. Votre login correspond à l'email saisi.");
                            repo.Connexion(client.Email, client.MotDePasse, delegate(string c, Exception err)
                            {
                                if (err != null)
                                {
                                    if (c == "1")
                                    {
                                        NavigationService.Navigate(new Uri("/Views/MainPage.xaml?id=" + client.Email, UriKind.Relative));
                                    }
                                }
                                else
                                    MessageBox.Show(err.Message);
                            });
                        }
                    }

                });

                //ViewModels.ClientViewModel client = new ViewModels.ClientViewModel(null, lpk_civilite.SelectedItem.ToString(), txt_nom.Text, txt_prenom.Text, txt_password.Password, txt_email.Text, txt_adresse.Text, txt_cp.Text, txt_ville.Text, txt_fixe.Text, txt_mobile.Text, (DateTime)dt_picker.Value);
                //try
                //{
                //    Model.Repository.CallWebService("http://localhost:8080/back-office/shopmob/addclient", client, delegate(UploadStringCompletedEventArgs u)
                //    {
                //        if (!String.IsNullOrEmpty(u.Result))
                //        {
                //            MessageBox.Show("Inscription réussie. Votre login correspond à l'email saisi.");
                //            if (repo.Connexion(client.Email, client.MotDePasse))
                //            {
                //                NavigationService.Navigate(new Uri("/Views/MainPage.xaml?id=" + client.Email, UriKind.Relative));
                //            }
                //        }
                //    });
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message);
                //}
            }
            else
            {
                MessageBox.Show(message);
            }
        }
    }
}